var sqlite3 = require("sqlite3");
sqlite3.verbose();
var sqlite = require("sqlite");
class SqliteUtil {
  constructor() {
    
  }
  // 打开数据库
  // 得到的db为拥有promise方法的db
  static async open(filename){
    return sqlite.open({
      filename: filename,
      driver: sqlite3.Database
    });
  }

  static async each(db,sql,params){
    return new Promise((resolve, reject) => {
      let data = [];
      db.getDatabaseInstance().each(sql, params, (err, row) => {
        if(err) {
          reject(err)
        } else {
          data.push(row);
        }
      },()=>{
        resolve(data);
      })
    });
  }

  static toUp(str){
    let newStr = '';
    let arr = str.split('_');
    arr.forEach((item,index) => {
      if(index>0){
        return newStr +=item.replace(item[0],item[0].toUpperCase());
      }else{
        return newStr += item;
      }
    });
    return newStr;
  }
  static dbObjectToEntity(dbObject,entity){
    let keys = Object.keys(dbObject);
    for(let i = 0;i<keys.length;i++){
      let entityKey = SqliteUtil.toUp(keys[i]);
      if(entity[entityKey]===undefined){
        entity[entityKey] = dbObject[keys[i]];
      }
    }
  }

}
SqliteUtil.dbPath="";
module.exports =  SqliteUtil;